package cn.itcast.bos.web.action.base;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.pagebean.PageBean;
import cn.itcast.bos.service.IDecidedzoneService;
import cn.itcast.bos.service.INoticebillService;
import cn.itcast.bos.service.IRegionService;
import cn.itcast.bos.service.IStaffService;
import cn.itcast.bos.service.ISubareaService;
import cn.itcast.bos.service.IUserService;
import cn.itcast.bos.service.IWorkordermanageService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
	
	protected T model;
	@Override
	public T getModel() {
		return model;
	}
	

	// 注入 service接口 属性
	@Resource
	protected IUserService userService;
	@Resource
	protected IStaffService staffService;
	@Resource
	protected IRegionService regionService;
	@Resource
	protected ISubareaService subareaService;
	@Resource
	protected IDecidedzoneService decidedzoneService;
	@Resource
	protected INoticebillService noticebillService;
	@Resource
	protected IWorkordermanageService workordermanageService;
	
	//分页显示提取
	public void setPage(int page){
		pageBean.setCurrentPage(page);
	}
	public void setRows(int rows){
		pageBean.setPageSize(rows);
	}	
	protected PageBean pageBean = new PageBean();
	protected DetachedCriteria detachedCriteria;
	
	public BaseAction() {
		/*ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();
		Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
		Class<T> domainClass = (Class<T>) actualTypeArguments[0];
		System.out.println(domainClass);
		*/
		ParameterizedType genericSuperclass=null;
		Type genericSuperclass2 = this.getClass().getGenericSuperclass();
		if (genericSuperclass2 instanceof ParameterizedType) {
			genericSuperclass=(ParameterizedType) genericSuperclass2;
		}else {
			genericSuperclass = (ParameterizedType) this.getClass().getSuperclass().getGenericSuperclass();
		}
		Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
		Class<T> domainClass = (Class<T>) actualTypeArguments[0];
		System.out.println(domainClass);
		//class cn.itcast.bos.domain.User
		
		//在构造方法中动态创建离线条件查询对象
		 detachedCriteria = DetachedCriteria.forClass(domainClass);
		 pageBean.setDetachedCriteria(detachedCriteria);
		try {
			model = domainClass.newInstance();
			System.out.println(model);   //cn.itcast.bos.domain.User@779a826
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void pageList(PageBean pageBean,String[] excludes){
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(excludes);
		JSONObject jsonObject = JSONObject.fromObject(pageBean, jsonConfig);
		String json = jsonObject.toString();
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		try {
			ServletActionContext.getResponse().getWriter().print(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void jsonList(List list,String[] excludes){
		JsonConfig jsonConfig = new JsonConfig();
		//String[] excludes=new String[]{"","",""};
		jsonConfig.setExcludes(excludes);
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		String json = jsonArray.toString();
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		try {
			ServletActionContext.getResponse().getWriter().print(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	// 将object对象序列化为json，并写回客户端浏览器 使用fastjson
		public void writeObjectFastjson(Object object,final String[] excludes) {

			/*JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.setExcludes(excludes);
			JSONObject jsonObject = JSONObject.fromObject(pageBean, jsonConfig);
			String json = jsonObject.toString();*/

			//自定义过滤器 过滤不想要的字段  // 方法二: 在要过滤的属性的get 方法上加注解@Serializerable(false)
			PropertyFilter filter = new PropertyFilter() {
				
				public boolean apply(Object object, String name, Object value) {
					for (String string : excludes) {
						//返回false 代表需要过滤
						if(name.equalsIgnoreCase(string)){
							return false;
						}
					}
					return true;
				}
			};
			
			//SerializerFeature.DisableCircularReferenceDetect 拒絕循环引用

			String json = JSON.toJSONString(object,filter,
					SerializerFeature.DisableCircularReferenceDetect);

			
			// 将json数据通过输出流写到客户端
			ServletActionContext.getResponse().setContentType(
					"text/json;charset=UTF-8");
			try {
				ServletActionContext.getResponse().getWriter().print(json);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		// 将object对象序列化为json，并写回客户端浏览器 使用fastjson
		public void selectwriteObjectFastjson(Object object,final String[] excludes) {
			PropertyFilter filter = new PropertyFilter() {
				
				public boolean apply(Object object, String name, Object value) {
					for (String string : excludes) {
						//返回false 代表需要过滤
						if(name.equalsIgnoreCase(string)){
							return true;
						}
					}
					return false;
				}
			};
			
			//SerializerFeature.DisableCircularReferenceDetect 拒絕循环引用

			String json = JSON.toJSONString(object,filter,
					SerializerFeature.DisableCircularReferenceDetect);

			
			// 将json数据通过输出流写到客户端
			ServletActionContext.getResponse().setContentType(
					"text/json;charset=UTF-8");
			try {
				ServletActionContext.getResponse().getWriter().print(json);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
}
